// pages/other/register/register.js
const app = getApp()
import { getTestingCode,sendTestingCode,sendUserLogin } from '../../../network/other'
import { toast,throttle } from '../../../utils/Tools'
Page({
  data: {
    phone: null,
    tabIndex:0,
    sms:null,
    text:'获取验证码',
    isSend: false,
    isDisabled:false
  },
  onLoad: function (options) {
    wx.hideHomeButton()
    wx.clearStorageSync()
    this.getCode = throttle(this.getCode,2000)
    this.handleChecking = throttle(this.handleChecking,2000)
    this.formSubmit = throttle(this.formSubmit,2000)
  },
  onShow(){
    console.log('rrrrewwwww')
    clearInterval(app.globalData.driverTask)
  },
  driver(){
    wx.reLaunch({
      url:'/driver_side/pages/other/login/login'
    })
  },
  //     事件函数       //
  handleText(){  //   处理按钮文字信息
    let timer;
    let number = 60
    this.setData({
      isSend:true
    })
    clearInterval(timer)
    timer = setInterval(()=>{
      if(number > 1){
        number--,
        this.setData({
          text:`${number}s 重新获取`
        })
      }else{
        this.setData({
          isSend:false,
          text:'获取验证码'
        })
        clearInterval(timer)
      }
    },1000)
  },
  handleClickTab(e){
    const {index} = e.currentTarget.dataset
    const tabIndex = Number(index)
    this.setData({tabIndex})
  },
  handleInputValue(e) {   //   手机输入框输入
    this.setData({
      phone: e.detail.value
    })
  },
  handleInput(e){
    e.detail.value ? this.setData({ isDisabled: false }) : this.setData({ isDisabled: true })
  },
  handleCode(e){ //   输入验证码
    this.setData({sms:e.detail})
  },
  async getCode() {  //   点击获取验证码
    if(/^1[3456789]\d{9}$/.test(this.data.phone) && this.data.phone.replace(/^\s+|\s+$/gm,'')){
      const res = await getTestingCode(this.data.phone);
      if (res && res.code === 200) {
        this.handleText()
        toast('获取验证码成功','success',1000)
        wx.setStorageSync('business_user', res.data);
        wx.setStorageSync('token', res.token);
        wx.setStorageSync('login_status','business')
        app.globalData.business_user = res.data
      }else{
        toast(res.msg)
      }
    }else{
      toast('手机号码错误,请重新输入')
    }
  },
  async handleChecking(){ //  处理点击完成验证
    if(this.data.phone&&this.data.sms){
      const res = await sendTestingCode(this.data.sms)
      if(res.code === 200){
        toast('登录成功','success')
        wx.setStorageSync('business_user', res.data);
        app.globalData.business_user = res.data
        wx.switchTab({
          url: '/pages/client/client/client'
        })
      }else{
        toast('验证码错误,请重新输入')
      }
    }else{
      toast('手机号码或者验证码错误')
    }
  },
  async formSubmit(e){ //  帐号登录点击验证
    const {username,password} = e.detail.value
    const data = {
      telephone: username,
      password
    }
    if(/^1[3456789]\d{9}$/.test(data.telephone) && data.telephone.replace(/^\s+|\s+$/gm,'')){
      const res = await sendUserLogin(data);
      if (res && res.code === 200) {
        wx.setStorageSync('business_user', res.data);
        wx.setStorageSync('token', res.token);
        wx.setStorageSync('login_status','business')
        app.globalData.business_user = res.data
        toast('登录成功','success')
        setTimeout(()=>{
          wx.switchTab({
            url: '/pages/client/client/client'
          })
        },1500)
      }else{
        toast(res.msg)
      }
    }else{
      toast('手机号码错误,请重新输入')
    }
  }
})